package cn.huixh.blindbox.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huixh
 * @version 1.0
 * @date 2022/5/5 10:26
 */
@Configuration
public class DruidConfig {
    @Bean
    public ServletRegistrationBean< StatViewServlet > statViewServlet() {
        ServletRegistrationBean< StatViewServlet > bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 这些参数可以在 com.alibaba.druid.support.http.StatViewServlet 的父类 com.alibaba.druid.support.http.ResourceServlet 中找到
        Map< String, String > initParams = new HashMap<>(8);

        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "123456");
        //默认就是允许所有访问
        initParams.put("allow", "");

        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * 配置一个web监控的filter
     *
     * @return 监控注册器bean
     */
    @Bean
    public FilterRegistrationBean< WebStatFilter > webStatFilter() {
        FilterRegistrationBean< WebStatFilter > bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());

        Map< String, String > initParams = new HashMap<>(8);

        initParams.put("exclusions", "*.js,*.css,/druid/*");

        bean.setInitParameters(initParams);

        bean.setUrlPatterns(List.of("/*"));

        return bean;
    }
}
